// pages/department/doctor/doctor.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorInfo: {},
    isLike: false
  },

  onClickLike(event) {
    console.log("onClickLike", event);

    let userInfo = app.globalData.userInfo;
    let myDoctor = userInfo.myDoctor;

    for (var i = 0; i < myDoctor.length; i++) {
      if (this.data.doctorInfo._id == myDoctor[i]._id) {
        return;
      }      
    }

    myDoctor.push(this.data.doctorInfo);
    userInfo.myDoctor = myDoctor;
    app.setGlobalData(userInfo);
    wx.cloud.callFunction({
      name: 'update_user_info',
      data: {
        userInfo: userInfo
      },
      success: res => {
        console.log("update user_info success", res);
      },
      fail: res => {
        console.log("update user_info fail", res);
      }
    })
  },

  onClickInquire(event) {
    console.log("onClickInquire", event);
    wx.navigateTo({
      url: '/pages/department/chat/chat?doctorInfo=' + JSON.stringify(this.data.doctorInfo)
    });

    // 将该医生加入到我的问诊
    let userInfo = app.globalData.userInfo;
    let myAsk = userInfo.myAsk;

    for (var i = 0; i < myAsk.length; i++) {
      if (this.data.doctorInfo._id == myAsk[i]._id) {
        flag = true;
        return;
      }      
    }

    myAsk.push(this.data.doctorInfo);
    userInfo.myAsk = myAsk;
    app.setGlobalData(userInfo);
    wx.cloud.callFunction({
      name: 'update_user_info',
      data: {
        userInfo: userInfo
      },
      success: res => {
        console.log("updata user_info success", res);
      },
      fail: res => {
        console.log("updata user_info fail", res);
      }
    });
    
  },

  // 生命周期函数--监听页面加载
  onLoad(options) {
    let doctorInfo = JSON.parse(decodeURIComponent(options.doctorInfo));
    this.setData({
      doctorInfo: doctorInfo
    });

    console.log("this doctorInfo", this.data.doctorInfo);
  },

  // 生命周期函数--监听页面初次渲染完成
  onReady() { },

  // 生命周期函数--监听页面显示
  onShow() { },

  // 生命周期函数--监听页面隐藏
  onHide() { },

  // 生命周期函数--监听页面卸载
  onUnload() { },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() { },

  // 页面上拉触底事件的处理函数
  onReachBottom() { },

  // 用户点击右上角分享
  onShareAppMessage() { }
})